/**
 *
 */
package com.ciwong.epaper.util;

import android.util.Log;


import java.math.BigDecimal;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.regex.Pattern;

/**
 * 日期转换工具类
 *
 * @author neo
 */
public class DateUtils {

    // 返回格式
    public final static String DATE_SHORT0 = "yyyy-MM-dd";
    public final static String DATE_SHORT = "yyyy/MM/dd";
    public final static String DATE_SHORT1 = "yyyy-M-d";
    public final static String DATE_SHORT2 = "yyyy.MM.dd";
    public final static String DATE_SHORT3 = "yyyy MM dd";
    public final static String DATE_SHORT4 = "yyyy年MM月dd日";
    public final static String DATE_SHORT5 = "yyyy年MM月dd日(EEEE)";
    public final static String DATE_SHORT6 = "yy-MM-dd";
    public final static String DATE_SHORT7 = "yyyy-M-d EEEE";
    public final static String DATE_SHORT8 = "MM月dd日(EEEE)";
    public final static String DATE_SHORT11 = "MM月dd日";
    public final static String DATE_SHORT9 = "yyyy年MM月dd日 HH时mm分";
    public final static String DATE_SHORT13 = "MM月dd日 HH:mm";
    public final static String DATE_SHORT10 = "yyyy 年 MM 月";
    public final static String DATE_SHORT12 = " MM ";
    public final static String DATE_SHORT14 = "MM月";
    public final static String DATE_SHORT15 = "MM月dd日 EEEE";


    public final static String DATE_LONG = "yyyy-MM-dd HH:mm:ss";
    public final static String DATE_LONG1 = "yyyy-M-d HH:m:s";
    public final static String DATE_LONG2 = "yyyy.MM.dd HH:mm:ss";
    public final static String DATE_LONG3 = "yyyy.MM.dd HH:mm:ss";
    public final static String DATE_LONG4 = "yyyy-MM-dd-HH-mm-ss";
    public final static String DATE_LONG5 = "MM-dd HH:mm";
    public final static String DATE_LONG6 = "yyyy-MM-dd HH:mm";
    public final static String DATE_LONG7 = "yyyy/MM/dd HH:mm";
    public final static String DATE_LONG8 = "MM/dd HH:mm";
    public final static String DATE_LONG9 = "HH:mm";
    public final static String DATE_LONG10 = "yyyy/MM";
    public final static String DATE_LONG11 = "yyyy-MM-dd";
    public final static String DATE_LONG12 = "yyyy.MM.dd HH:mm";

    public final static String DATE_SHORT_DEFAULT = DATE_SHORT;
    public final static String DATE_LONG_DEFAULT = DATE_LONG;

    // private static GregorianCalendar CAL = new GregorianCalendar();
    private static final SimpleDateFormat DATE_FORMATER_HOUR = new SimpleDateFormat("HH:mm");

    public static String getTimezoneLabel() {
        return new SimpleDateFormat("(z)").format(new Date());
    }

    public static String formatInHour(Date date) {
        return DATE_FORMATER_HOUR.format(date);
    }


    public static int calculateDifference(Date a, Date b) {

        int tempDifference = 0;
        int difference = 0;
        Calendar earlier = Calendar.getInstance();
        Calendar later = Calendar.getInstance();

        if (a.compareTo(b) < 0) {
            earlier.setTime(a);
            later.setTime(b);
        } else {
            earlier.setTime(b);
            later.setTime(a);
        }

        while (earlier.get(Calendar.YEAR) != later.get(Calendar.YEAR)) {
            tempDifference = 365 * (later.get(Calendar.YEAR) - earlier.get(Calendar.YEAR));
            difference += tempDifference;
            earlier.add(Calendar.DAY_OF_YEAR, tempDifference);
        }

        if (earlier.get(Calendar.DAY_OF_YEAR) != later.get(Calendar.DAY_OF_YEAR)) {
            tempDifference = later.get(Calendar.DAY_OF_YEAR) - earlier.get(Calendar.DAY_OF_YEAR);
            difference += tempDifference;
            earlier.add(Calendar.DAY_OF_YEAR, tempDifference);
        }

        return difference;
    }


    /*
     * Format date as the requirement by server (2011-01-02)
     */
    public static String formatDateForServer(java.util.Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("", Locale.SIMPLIFIED_CHINESE);
        sdf.applyPattern("yyyy-MM-dd");
        return sdf.format(date);
    }

    /*
     * Format date and time as the requirement by server ( 2011-01-02 01:02:03)
     */
    public static String formatDateTimeForServer(java.util.Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("", Locale.SIMPLIFIED_CHINESE);
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static String formatDateTimeForServer(java.util.Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat("", Locale.SIMPLIFIED_CHINESE);
        sdf.applyPattern(format);
        return sdf.format(date);
    }

    static public java.util.Date ParseDate(String str) {
        try {
            java.util.Date dt;
            SimpleDateFormat sdf = new SimpleDateFormat("", Locale.SIMPLIFIED_CHINESE);
            if (str.length() > ("yyyy-MM-dd".length() + 2))
                sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
            else
                sdf.applyPattern("yyyy-MM-dd");
            dt = sdf.parse(str, new ParsePosition(0));

            return dt;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @param fromType 你传入时间的格式
     * @param toType   你想转的格式
     * @param fromDate 你想处理的时间
     * @return 返回处理结果，String
     */
    public static String Date2String(String fromType, String toType, String fromDate) {
        String toDateStr = "";
        Date tempDate = null;
        SimpleDateFormat sdfFrom = null;
        SimpleDateFormat sdfTo = null;
        try {
            sdfFrom = new SimpleDateFormat(fromType);
            tempDate = sdfFrom.parse(fromDate);
        } catch (Exception e) {
            e.printStackTrace();
            tempDate = new Date();
        }

        sdfTo = new SimpleDateFormat(toType);
        toDateStr = sdfTo.format(tempDate);
        return toDateStr;
    }

    public static String Date2String(Date fromDate, String toType) {
        String toDateStr = "";
        if (null != fromDate) {
            SimpleDateFormat sdfFrom = null;
            sdfFrom = new SimpleDateFormat(toType);
            toDateStr = sdfFrom.format(fromDate);
        }
        return toDateStr;
    }

    public static String Date2StringLongDefault(Date fromDate) {
        String toDateStr = "";
        SimpleDateFormat sdfFrom = null;
        sdfFrom = new SimpleDateFormat(DATE_LONG_DEFAULT);
        toDateStr = sdfFrom.format(fromDate);
        return toDateStr;
    }

    public static String Date2StringShortDefault(Date fromDate) {
        String toDateStr = "";
        SimpleDateFormat sdfFrom = null;
        sdfFrom = new SimpleDateFormat(DATE_SHORT_DEFAULT);
        toDateStr = sdfFrom.format(fromDate);
        return toDateStr;
    }

    /**
     * 获取系统当前时间, 默认时间格式为yyyy-MM-dd-HH-mm-ss
     *
     * @return 返回处理结果，String
     */
    public static String nowDate() {
        return new SimpleDateFormat(DATE_LONG4).format(Calendar.getInstance().getTime());
    }

    public static String nowDate(String format) {
        return new SimpleDateFormat(format).format(Calendar.getInstance().getTime());
    }

    public static Date String2Date(String fromType, String dateStr) {
        Date date = null;
        SimpleDateFormat format = new SimpleDateFormat(fromType);
        try {
            date = format.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
            // 转行错误则返回当前系统日期
            Log.d("temp", "##### String2Date#e.printStackTrace()#######");
            date = Calendar.getInstance().getTime();
        }
        return date;

    }

    /**
     * 转换字符串格式的日期 到另一种日期格式，以字符串的形式输出
     *
     * @param source   输入源
     * @param fromType 输入源所属的日期格式
     * @param toType   输出的格式化
     * @return 格式化后的日期字串
     */
    public static String String2typeStr(String source, String fromType, String toType) {
        Date date = String2Date(fromType, source);
        return Date2String(date, toType);
    }

    // DATE_LONG4 to DATE_SHORT4
    public static String dateFormat(String date) {
        if (date.length() > 11)
            date = date.substring(0, 10);
        Date curDate = Calendar.getInstance().getTime();
        String thisYear = DateUtils.Date2String(curDate, DateUtils.DATE_SHORT);
        thisYear = thisYear.substring(0, 4);
        String ret = DateUtils.String2typeStr(date, DateUtils.DATE_SHORT1, DateUtils.DATE_SHORT4);
        if (ret.startsWith(thisYear))
            ret = ret.substring(5);
        return ret;
    }

    /**
     * 根据日期获得星期
     *
     * @param date
     * @return
     */
    public static String getWeekOfDate(Date date) {
        String[] weekDaysName = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return weekDaysName[intWeek];
    }

    /**
     * 获取某天的距离某天的日期
     *
     * @param date 某天所对应的日期
     * @param from 该值为正值表示查找date几天后的日期 负值代表表示查找date几天前的日期
     * @return
     */
    public static Date getDayFromNow(Date date, int from) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE, from);// 把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime(); // 这个时间就是日期往后推一天的结果\

        return date;
    }

    /**
     * 获取的距离当前系统日期的若干天的日期
     *
     * @param from 该值为正值表示查找当前系统日期几天后的日期 负值代表表示查找当前系统日期几天前的日期
     * @return
     */
    public static Date getDayFromNow(int from) {
        Date date = new Date();

        /*
         * if(from==0) return date;
         */
        date = getDayFromNow(date, from);
        return date;
    }

    /**
     * @param day1
     * @param day2
     * @return 0 如果两时间相等返回0，如果day1 早于 day2返回-1，如果day1晚于day2, 返回1
     */

    public static int compareDate(Date day1, Date day2) {

        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();

        if (day1 != null && day2 != null) {
            cal1.setTime(day1);
            cal2.setTime(day2);
        }

        return cal1.compareTo(cal2);
    }

    public static long dayFromNow(Date date) {
        if (date == null)
            return 0;
        long dayCount = (date.getTime() - System.currentTimeMillis() + (1000 * 3600 * 24) - 1) / (1000 * 3600 * 24);
        return dayCount;
    }


    /**
     * =======
     * public static long dayFromNow(Date date) {
     * if (date == null)
     * return 0;
     * long dayCount = (date.getTime() - System.currentTimeMillis()
     * + (1000 * 3600 * 24) - 1)
     * / (1000 * 3600 * 24);
     * return dayCount;
     * }
     * <p>
     * public static String getTimeString(Date startTime) {
     * String timeString = "";
     * <p>
     * Long createTime = startTime.getTime();
     * long currentTime = System.currentTimeMillis();
     * long intervalTime = (currentTime - createTime) / 1000;
     * <p>
     * //如果是在一天内发布的。
     * if (intervalTime < 60 * 60 * 24) {
     * if (intervalTime < 60) {
     * timeString = AppManager.getInstance().getResources().getString(R.string.moment_create_time_justnow);
     * } else if (intervalTime < 60 * 60) {
     * timeString = AppManager.getInstance().getResources().getString(R.string.moment_create_time_minute, intervalTime / 60);
     * } else if (intervalTime < 60 * 60 * 24) {
     * timeString = AppManager.getInstance().getResources().getString(R.string.moment_create_time_hour, intervalTime / 60 / 60);
     * }
     * } else if ((intervalTime >= 60 * 60 * 24) && intervalTime <= 60 * 60 * 24 * 2) {//如果是在昨天发布的
     * timeString = AppManager.getInstance().getResources().getString(R.string.moment_create_time_yesterday);
     * } else if (intervalTime > 60 * 60 * 24 * 2) {//如果是在昨天前发布的
     * timeString = DateUtils.Date2StringShortDefault(startTime);
     * }
     * <p>
     * return timeString;
     * }
     * <p>
     * <p>
     * /**
     * 获取两个日期之间的间隔天数
     *
     * @return
     */
    public static int getIntervalCount(Date startDate, Date endDate) {
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setTime(startDate);
        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
        fromCalendar.set(Calendar.MINUTE, 0);
        fromCalendar.set(Calendar.SECOND, 0);
        fromCalendar.set(Calendar.MILLISECOND, 0);

        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(endDate);
        toCalendar.set(Calendar.HOUR_OF_DAY, 0);
        toCalendar.set(Calendar.MINUTE, 0);
        toCalendar.set(Calendar.SECOND, 0);
        toCalendar.set(Calendar.MILLISECOND, 0);

        return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));
    }


    /**
     * 获取两个日期之间的间隔天数
     *
     * @return
     */
    public static int getIntervalCount2(Date startDate, Date endDate) {
        return (int) ((endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24));
    }

    /**
     * >>>>>>> .r196
     * 判断是否为合法的日期时间字符串
     *
     * @param str_input
     * @param str_input
     * @return boolean;符合为true,不符合为false
     */
    public static boolean isDate(String str_input, String rDateFormat) {
        if (!isNull(str_input)) {
            SimpleDateFormat formatter = new SimpleDateFormat(rDateFormat);
            formatter.setLenient(false);
            try {
                formatter.format(formatter.parse(str_input));
            } catch (Exception e) {
                return false;
            }
            return true;
        }
        return false;
    }

    public static boolean isNull(String str) {
        if (str == null)
            return true;
        else
            return false;
    }


    //    /**
    //     * 获取当前系统的年
    //     */
    //    public static String getYear() {
    //        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
    //        return sdf.format(new java.util.Date());
    //    }
    // 获取某年的第几周的开始日期
    public static Date getFirstDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);

        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);

        return getFirstDayOfWeek(cal.getTime());
    }

    /**
     * 获取当前系统的日
     */
    public static int getDay() {
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }


    /**
     * 再判断该课程的起止时间:时间还未到课程开课的时候：返回false，其他时候返回true
     */
    public static boolean getTimeBoolean(long startTime, long endTime, long nowTime) {
        boolean isGet;
        //开始时间大于2个小时且结束时间也比结束时间大
        if (startTime - 2 * 60 * 60 * 1000 > nowTime) {
            isGet = false;
        } else {
            isGet = true;
        }
        return isGet;
    }


    /**
     * 获取 当前年、半年、季度、月、日、小时 开始结束时间
     */

    private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
    private final static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");
    ;
    private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    ;


    /**
     * 获得本周的第一天，周一
     *
     * @return
     */
    public static Date getCurrentWeekDayStartTime() {
        Calendar c = Calendar.getInstance();
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
            c.add(Calendar.DATE, -weekday);
            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }

    /**
     * 获得本周的最后一天，周日
     *
     * @return
     */
    public static Date getCurrentWeekDayEndTime() {
        Calendar c = Calendar.getInstance();
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK);
            c.add(Calendar.DATE, 8 - weekday);
            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }

    /**
     * 获得本天的开始时间，即2012-01-01 00:00:00
     *
     * @return
     */
    public static Date getCurrentDayStartTime() {
        Date now = new Date();
        try {
            now = shortSdf.parse(shortSdf.format(now));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 获得本天的结束时间，即2012-01-01 23:59:59
     *
     * @return
     */
    public static Date getCurrentDayEndTime() {
        Date now = new Date();
        try {
            now = longSdf.parse(shortSdf.format(now) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 获得本小时的开始时间，即2012-01-01 23:59:59
     *
     * @return
     */
    public static Date getCurrentHourStartTime() {
        Date now = new Date();
        try {
            now = longHourSdf.parse(longHourSdf.format(now));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 获得本小时的结束时间，即2012-01-01 23:59:59
     *
     * @return
     */
    public static Date getCurrentHourEndTime() {
        Date now = new Date();
        try {
            now = longSdf.parse(longHourSdf.format(now) + ":59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 获得本月的开始时间，即2012-01-01 00:00:00
     *
     * @return
     */
    public static Date getCurrentMonthStartTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.DATE, 1);
            now = shortSdf.parse(shortSdf.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 当前月的结束时间，即2012-01-31 23:59:59
     *
     * @return
     */
    public static Date getCurrentMonthEndTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.DATE, 1);
            c.add(Calendar.MONTH, 1);
            c.add(Calendar.DATE, -1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 当前年的开始时间，即2012-01-01 00:00:00
     *
     * @return
     */
    public static Date getCurrentYearStartTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.MONTH, 0);
            c.set(Calendar.DATE, 1);
            now = shortSdf.parse(shortSdf.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 当前年的结束时间，即2012-12-31 23:59:59
     *
     * @return
     */
    public static Date getCurrentYearEndTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.set(Calendar.MONTH, 11);
            c.set(Calendar.DATE, 31);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 当前季度的开始时间，即2012-01-1 00:00:00
     *
     * @return
     */
    public static Date getCurrentQuarterStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 当前季度的结束时间，即2012-03-31 23:59:59
     *
     * @return
     */
    public static Date getCurrentQuarterEndTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 8);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 获取前/后半年的开始时间
     *
     * @return
     */
    public static Date getHalfYearStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 0);
            } else if (currentMonth >= 7 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 6);
            }
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;

    }

    /**
     * 获取前/后半年的结束时间
     *
     * @return
     */
    public static Date getHalfYearEndTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }


    /**
     * 取得当前日期是多少周
     *
     * @param date
     * @return
     */
    public static int getWeekOfYear(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setMinimalDaysInFirstWeek(7);
        c.setTime(date);
        return c.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 得到某一年周的总数
     *
     * @param year
     * @return
     */
    public static int getMaxWeekNumOfYear(int year) {
        Calendar c = new GregorianCalendar();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
        return getWeekOfYear(c.getTime());
    }

    public static int getYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    /**
     * 得到某年某月的第一天
     *
     * @param year
     * @return
     */
    public static boolean isFirstDayOfMonthIsSunDay(int year, int month) {

        Calendar d = new GregorianCalendar();
        d.set(Calendar.YEAR, year);
        d.set(Calendar.MONTH, month);
        d.set(Calendar.DAY_OF_MONTH, 1);


        int i = d.get(Calendar.DAY_OF_WEEK);
        Log.e("TAG", "isFirstDayOfMonthIsSunDay: " + i);
        // 获取年
        int yearnew = d.get(Calendar.YEAR);

        // 获取月，这里需要需要月份的范围为0~11，因此获取月份的时候需要+1才是当前月份值
        int monthnew = d.get(Calendar.MONTH) + 1;
        // 获取日
        int daynew = d.get(Calendar.DAY_OF_WEEK);
        Log.e("TAG", "现在是" + yearnew + "年" + monthnew + "月" + daynew);
        if (i == 1) {
            return true;
        }
        return false;

    }


    /**
     * 得到某年某周的最后一天
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getLastDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);
        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);
        String firstDateStr = DateUtils.Date2String(c.getTime(), DATE_LONG7);
        //		firstDateStr = firstDateStr + " " + "00:00";
        Log.d("temp", "#######int year, int week  ###########" + year + ":" + week);
        Log.d("temp", "#######int year, int week static getLastDayOfWeek###########" + firstDateStr);
        return getLastDayOfWeek(cal.getTime());
    }

    /**
     * 取得指定日期所在周的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday

        String firstDateStr = DateUtils.Date2String(c.getTime(), DATE_LONG7);
        //		firstDateStr = firstDateStr + " " + "00:00";
        Log.d("temp", "#######static getFirstDayOfWeek###########" + firstDateStr);
        return c.getTime();
    }

    /**
     * 取得指定日期所在周的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday

        String firstDateStr = DateUtils.Date2String(c.getTime(), DATE_LONG7);
        //		firstDateStr = firstDateStr + " " + "00:00";
        Log.d("temp", "#######static getLastDayOfWeek###########" + firstDateStr);
        //		firstDateStr = firstDateStr + " " + "00:00";
        Log.d("temp", "#######static getFirstDayOfWeek###########" + firstDateStr);
        return c.getTime();
    }

    public static int getDaysCountOfMonth() {
        int days = 0;
        Calendar instance = Calendar.getInstance();
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH);
        return getDaysCountOfMonth(year, month);
    }

    public static int getDaysCountOfMonth(int year, int month) {
        int day = 0;
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            day = 29;
        } else {
            day = 28;
        }
        month++;
        if (month <= 0) {
            month = 12;
        }
        if (month >= 13) {
            month = 1;
        }

        Log.e("TAG", "getMonthOfDay: " + month);
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                return day;

        }

        return 0;
    }

    public static String getMonthString() {
        String[] weekDaysName = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);
        return weekDaysName[month];
    }

    public static int getWeekDay() {
        Calendar calendar = Calendar.getInstance();
        int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
        if (weekDay == 1) {
            return 6;
        } else if (weekDay == 2) {
            return 0;
        } else if (weekDay == 3) {
            return 1;
        } else if (weekDay == 4) {
            return 2;
        } else if (weekDay == 5) {
            return 3;
        } else if (weekDay == 6) {
            return 4;
        } else if (weekDay == 7) {
            return 5;
        }
        return weekDay;
    }

    public static boolean isToday(Date date) {
        try {
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
            if (fmt.format(date).toString().equals(fmt.format(new Date()).toString())) {//格式化为相同格式
                return true;
            }
        } catch (Exception e) {
        }
        return false;
    }

    /**
     * 判断整数（int）
     */
    public static boolean isInteger(String number) {
        if ("".equals(number) && number == null) {
            return false;
        }
        if (number.indexOf(".") != -1) {
            String[] numArr = number.split("\\.");
            if (numArr.length >= 2) {
                int lastNum = 0;
                try {
                    lastNum = Integer.parseInt(numArr[1]);
                } catch (Exception e) {

                }
                if (lastNum > 0) {
                    return false;
                } else {
                    return true;
                }
            } else {
                return false;
            }
        } else {
            //整数
            return true;
        }
    }
}
